/*
 * Copyright (C) 2011, Pavel Samko <bulldozerbsg@gmail.com>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the "Pavel Samko" nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

#ifndef VCNET_H
#define VCNET_H

#include "VCStd.h"

#if VC_PLATFORM_WIN
#include <windows.h>
#include <winsock.h>

typedef SOCKET VC_SOCKET;
typedef struct sockaddr VC_SOCKET_ADDR;
typedef struct sockaddr_in VC_SOCKET_ADDR_IN;
#define VC_SOCKET_INVALID INVALID_SOCKET
#define VC_SOCKET_ERROR SOCKET_ERROR
#define VC_MAX_CONN SOMAXCONN

#define socklen_t int
#else
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>

typedef int VC_SOCKET;
typedef struct sockaddr VC_SOCKET_ADDR;
typedef struct sockaddr_in VC_SOCKET_ADDR_IN;
#define VC_SOCKET_INVALID -1
#define VC_SOCKET_ERROR -1
#define VC_MAX_CONN SOMAXCONN
#endif

//bool operator == (const VC_SOCKET_ADDR_IN & addr1, const VC_SOCKET_ADDR_IN & addr2);
//bool operator < (const VC_SOCKET_ADDR_IN & addr1, const VC_SOCKET_ADDR_IN & addr2);

bool VCNetInit();
void VCNetDeinit();

VC_SOCKET VCNetSocketTCP();
void VCNetClose(VC_SOCKET s);
bool VCNetSetNonBlock(VC_SOCKET s, bool nonblock = true);

bool VCNetGetSockName(VC_SOCKET s, VC_SOCKET_ADDR_IN * addr);

bool VCNetBind(VC_SOCKET s, const VC_SOCKET_ADDR_IN * addr);
bool VCNetConnect(VC_SOCKET s, const VC_SOCKET_ADDR_IN * addr);
bool VCNetListen(VC_SOCKET s, int max_conn = VC_MAX_CONN);
VC_SOCKET VCNetAccept(VC_SOCKET s, VC_SOCKET_ADDR_IN * addr);

bool VCNetSend(VC_SOCKET s, void * buff, size_t * buff_size);
bool VCNetRecv(VC_SOCKET s, void * buff, size_t * buff_size);

unsigned short VCNetHTONS(unsigned short x);
unsigned short VCNetNTOHS(unsigned short x);
unsigned long VCNetHTONL(unsigned long x);
unsigned long VCNetNTOHL(unsigned long x);

bool VCNetInitAddrIn(VC_SOCKET_ADDR_IN * addr, const char * addr_str, unsigned short port);
const char * VCNetGetIp(const VC_SOCKET_ADDR_IN * addr);
unsigned short VCNetGetPort(const VC_SOCKET_ADDR_IN * addr);

unsigned short VCNetGetPort(VC_SOCKET s);

#endif /* VCNET_H */

